package supertrunfo;

import daos.LoginDAOS;
import java.io.File;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;
import relatorio.Repositorio;
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class Principal extends javax.swing.JFrame {

    private static ResourceBundle myResources;
    private static final Logger log = Logger.getLogger(Principal.class.getName());

    /** Creates new form Principal */
    public Principal() {

        try {

            Handler console = new ConsoleHandler();
            Handler file = new FileHandler("C:\\SuperTropa.log");

            /*Define que na consola apenas aparece log com nível superior ou
              a warning e no ficheiro deve aparecer o log de qualquer nível
             */

            console.setLevel(Level.INFO);
            file.setLevel(Level.ALL);
            //Define o formato de output do ficheiro como XML
            file.setFormatter(new SimpleFormatter());
            //Adiciona os handlers para ficheiro e console
            log.addHandler(file);
            log.addHandler(console);
            //Ignora os Handlers definidos no Logger Global
            log.setUseParentHandlers(false);
        }
        catch(IOException io){
            log.warning("O ficheiro hellologgin.xml não pode ser criado");
        }
        setVisible(true);
        if (Login.LinguaAtual.equals("Português")) {
           myResources =  ResourceBundle.getBundle("Message_pt_BR",Locale.getDefault());
        }
        else {
           myResources =  ResourceBundle.getBundle("Message_en_US",Locale.ENGLISH);
        }
        log.info("Tela principal iniciada.");
        initComponents();
       /* desktop = new JDesktopPane() {
        public void paintComponent (Graphics g){
        ImageIcon img = new ImageIcon("imagem.jpg");
        g.drawImage(img.getImage(),0,0,null);
        super.paintComponent(g);
        }};
        desktop.setOpaque(false);
        this.setContentPane(desktop);*/
        setLocationRelativeTo(null);

        lNovoJ.setToolTipText(myResources.getString("principal.infInicio"));
        lPerfil.setToolTipText(myResources.getString("principal.infEntrar"));
        lRelatorio.setToolTipText(myResources.getString("principal.infGerar"));
        lAjuda.setToolTipText(myResources.getString("principal.infAbrir"));
        lLogoff.setToolTipText(myResources.getString("principal.infLogoff"));
        
        
    }

    JasperPrint relat;
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        desktop = new javax.swing.JDesktopPane();
        tb1 = new javax.swing.JToolBar();
        lNovoJ = new javax.swing.JLabel();
        lPerfil = new javax.swing.JLabel();
        lRelatorio = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lAjuda = new javax.swing.JLabel();
        lLogoff = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu6 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem11 = new javax.swing.JMenuItem();
        jMenuItem12 = new javax.swing.JMenuItem();
        jMenu5 = new javax.swing.JMenu();
        jMenuItem7 = new javax.swing.JCheckBoxMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem14 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));

        desktop.setBackground(new java.awt.Color(255, 255, 255));
        desktop.setBorder(new javax.swing.border.MatteBorder(new javax.swing.ImageIcon("C:\\Users\\RENAM\\Documents\\NetBeansProjects\\SuperTrunfo\\imagem.jpg"))); // NOI18N

        tb1.setRollover(true);
        tb1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        tb1.setEnabled(false);

        lNovoJ.setIcon(new javax.swing.ImageIcon(getClass().getResource("/supertrunfo/package_games_32x32.png"))); // NOI18N
        lNovoJ.setText(" ");
        lNovoJ.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lNovoJ.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lNovoJMouseClicked(evt);
            }
        });
        tb1.add(lNovoJ);

        lPerfil.setIcon(new javax.swing.ImageIcon("C:\\Users\\RENAM\\Downloads\\personal_32x32.png")); // NOI18N
        lPerfil.setText(" ");
        lPerfil.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lPerfil.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lPerfilMouseClicked(evt);
            }
        });
        tb1.add(lPerfil);

        lRelatorio.setIcon(new javax.swing.ImageIcon("C:\\Users\\RENAM\\Downloads\\log_32x32.png")); // NOI18N
        lRelatorio.setText(" ");
        lRelatorio.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lRelatorio.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lRelatorioMouseClicked(evt);
            }
        });
        tb1.add(lRelatorio);
        tb1.add(jLabel4);

        lAjuda.setIcon(new javax.swing.ImageIcon("C:\\Users\\RENAM\\Downloads\\khelpcenter_32x32.png")); // NOI18N
        lAjuda.setText(" ");
        lAjuda.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lAjuda.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lAjudaMouseClicked(evt);
            }
        });
        tb1.add(lAjuda);

        lLogoff.setIcon(new javax.swing.ImageIcon("C:\\Users\\RENAM\\Downloads\\exit_32x32.png")); // NOI18N
        lLogoff.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lLogoff.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lLogoffMouseClicked(evt);
            }
        });
        tb1.add(lLogoff);

        jMenu1.setText(myResources.getString("principal.arquivo"));

        jMenu6.setText(myResources.getString("principal.partida"));

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
        jMenuItem2.setText(myResources.getString("principal.herois"));
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem2);

        jMenuItem9.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3, 0));
        jMenuItem9.setText(myResources.getString("principal.personagem"));
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem9);

        jMenu1.add(jMenu6);

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0));
        jMenuItem1.setText(myResources.getString("principal.logoff"));
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem5.setText(myResources.getString("principal.exit"));
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem5);

        jMenuBar1.add(jMenu1);

        jMenu3.setText(myResources.getString("principal.conta"));

        jMenu2.setText(myResources.getString("principal.relatorio"));

        jMenuItem11.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, 0));
        jMenuItem11.setText(myResources.getString("principal.historico"));
        jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem11ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem11);

        jMenuItem12.setText(myResources.getString("principal.classificacao"));
        jMenu2.add(jMenuItem12);

        jMenu3.add(jMenu2);

        jMenu5.setText(myResources.getString("principal.usuario"));

        jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        jMenuItem7.setSelected(true);
        jMenuItem7.setText(myResources.getString("principal.alterarC"));
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem7);

        jMenuItem10.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
        jMenuItem10.setText(myResources.getString("principal.desabilitar"));
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed1(evt);
            }
        });
        jMenu5.add(jMenuItem10);

        jMenu3.add(jMenu5);

        jMenuBar1.add(jMenu3);

        jMenu4.setText(myResources.getString("principal.ajuda"));
        jMenu4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu4ActionPerformed(evt);
            }
        });

        jMenuItem14.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F6, 0));
        jMenuItem14.setText(myResources.getString("principal.manual"));
        jMenu4.add(jMenuItem14);

        jMenuBar1.add(jMenu4);

        setJMenuBar(jMenuBar1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(tb1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1010, Short.MAX_VALUE)
            .add(desktop, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1010, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(tb1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 36, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(desktop, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 542, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        this.hide();// TODO add your handling code here:
        new Login();
        log.info("Saída da tela principal, retorno ao login.");
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem10ActionPerformed
        ExcluirConta ec=new ExcluirConta(myResources.getString("principal.excluir"),true,true,false,false);
        log.info("Solicitação para excluir.");
	desktop.add(ec);// TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem10ActionPerformed

    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem9ActionPerformed
        //JogoST jogo=new JogoST("Super Tropa",true,true,false,false);
	//desktop.add(jogo);
    }//GEN-LAST:event_jMenuItem9ActionPerformed

    private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem11ActionPerformed
        try {
                int id = LoginDAOS.idLog();
        	Repositorio rep = new Repositorio();
		relat = rep.historico(id);
		JasperViewer.viewReport(relat, false);
                log.info("Relatorio de historico gerado com sucesso.");
	} catch (Exception e) {
                log.info("Erro ao gerar relatório.");
		JOptionPane.showMessageDialog(null, myResources.getString("principal.erro") + e.getMessage());// TODO add your handling code here:
        }
    }//GEN-LAST:event_jMenuItem11ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        JogoSTM jogo=new JogoSTM(myResources.getString("principal.supertropa"),true,true,false,false);
	desktop.add(jogo);// TODO add your handling code here:
        log.info("Tela de jogo requisitada.");
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenu4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu4ActionPerformed
        File pdf = new File("manual.pdf");// TODO add your handling code here:
        log.info("Manual de instruções aberto.");
    }//GEN-LAST:event_jMenu4ActionPerformed

    private void jMenuItem10ActionPerformed1(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem10ActionPerformed1
        ExcluirConta ec=new ExcluirConta(myResources.getString("principal.excluir"),true,true,false,false);
	desktop.add(ec);// TODO add your handling code here:
        log.info("Solicitação para excluir.");
    }//GEN-LAST:event_jMenuItem10ActionPerformed1

    private void lNovoJMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lNovoJMouseClicked
        JogoSTM jogo=new JogoSTM(myResources.getString("principal.supertropa"),true,true,false,false);
	desktop.add(jogo);
        log.info("Tela de jogo requisitada.");
    }//GEN-LAST:event_lNovoJMouseClicked

    private void lPerfilMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lPerfilMouseClicked
        AlteraConta ac=new AlteraConta(myResources.getString("principal.alterar"),true,true,false,false);
	desktop.add(ac);// TODO add your handling code here:
        log.info("Tela de alteração requisitada.");
    }//GEN-LAST:event_lPerfilMouseClicked

    private void lRelatorioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lRelatorioMouseClicked
        try {
                int id = LoginDAOS.idLog();
        	Repositorio rep = new Repositorio();
		relat = rep.historico(id);
		JasperViewer.viewReport(relat, false);
                log.info("Relatorio de historico gerado com sucesso.");
	} catch (Exception e) {
                log.info("Erro ao gerar relatório.");
		JOptionPane.showMessageDialog(null, myResources.getString("principal.erro") + e.getMessage());// TODO add your handling code here:
        }
    }//GEN-LAST:event_lRelatorioMouseClicked

    private void lAjudaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lAjudaMouseClicked
        try
        {
        java.awt.Desktop desktop2 = java.awt.Desktop.getDesktop();
        desktop2.open(new File("C:\\arquivo.pdf"));
        }catch(Exception e)
        {
            
        }
    }//GEN-LAST:event_lAjudaMouseClicked

    private void lLogoffMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lLogoffMouseClicked
        setVisible(false);// TODO add your handling code here:
        new Login();
        log.info("Saída da tela principal, retorno ao login.");
    }//GEN-LAST:event_lLogoffMouseClicked

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        log.info("Saída do programa supertropa 2.");
        System.exit(0);// TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        AlteraConta ac=new AlteraConta(myResources.getString("principal.alterar"),true,true,false,false);
	desktop.add(ac);// TODO add your handling code here:
        log.info("Tela de alteração requisitada.");
    }//GEN-LAST:event_jMenuItem7ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane desktop;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem14;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JCheckBoxMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JLabel lAjuda;
    private javax.swing.JLabel lLogoff;
    private javax.swing.JLabel lNovoJ;
    private javax.swing.JLabel lPerfil;
    private javax.swing.JLabel lRelatorio;
    private javax.swing.JToolBar tb1;
    // End of variables declaration//GEN-END:variables

}
